﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace App
{
    public class WaterMarkerTextBoxDecorector
    {
        public WaterMarkerTextBoxDecorector(TextBox textbox, string hintText)
            :this(textbox, hintText, Color.Gray)
        {
        }

        public WaterMarkerTextBoxDecorector(TextBox textbox, string hintText, Color noTextColor)
        {
            this.textbox = textbox;
            this.hintText = hintText;
            this.normalColor = textbox.ForeColor;
            this.noTextColor = noTextColor;
            textbox.GotFocus += new EventHandler(textbox_GotFocus);
            textbox.LostFocus += new EventHandler(textbox_LostFocus);

            if (textbox.Focused)
                textbox_GotFocus(textbox, EventArgs.Empty);
            else
                textbox_LostFocus(textbox, EventArgs.Empty);
        }

        void textbox_LostFocus(object sender, EventArgs e)
        {
            hasText = ! string.IsNullOrEmpty(textbox.Text);
            if (!hasText)
            {
                textbox.ForeColor = noTextColor;
                textbox.Text = hintText;
            }
        }

        void textbox_GotFocus(object sender, EventArgs e)
        {
            if (!hasText)
            {
                textbox.Text = string.Empty;
                textbox.ForeColor = normalColor;
            }
        }

        private TextBox textbox;
        private string hintText;
        private bool hasText;
        private Color noTextColor;
        private Color normalColor;

        public string HintText
        {
            get { return hintText; }
            set { hintText = value; }
        }

        public TextBox TheTextBox
        {
            get { return textbox; }
        }

    }
}
